<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="imageUrl" :src="imageUrl" class="avatar">

      <i v-else class="el-icon-plus avatar-uploader-icon" />

      <!-- 加载插件 -->
      <el-progress v-if="showProgress" type="circle" :percentage="percent" class="progress" />
    </el-upload>
  </div>
</template>

<script>

const COS = require('cos-js-sdk-v5')
// 配置秘钥
const cos = new COS({
  SecretId: 'AKIDDH6g18zby5ybz70d1A4i6F8mB2e180cz', // 身份识别ID
  SecretKey: 'A4i6F8l9IBpEOIxMcdOuABwRhhSzGJ6d' // 身份秘钥
})
export default {
  name: 'UploadImg',
  props: {
    // url: {
    //   type: String,
    //   default: ''
    // },
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      imageUrl: '',
      // 加载进度
      percent: 0,
      // 控制进度条显示与隐藏
      showProgress: false
    }
  },
  watch: {
    // url: function() {
    //   this.imageUrl = this.url
    // },
    value: function() {
      this.imageUrl = this.value
    }
  },
  methods: {
    upload(res) {
      if (res.file) {
        this.showProgress = true
        // 执行上传操作
        cos.putObject({
          Bucket: 'xiaosu-1313015816', /* 存储桶 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file, // 上传文件对象
          onProgress: (progressData) => {
            // console.log(JSON.stringify(progressData))
            this.percent = progressData.percent * 100
          }
        }, (err, data) => {
          console.log(err || data)
          // 上传成功之后
          if (data.statusCode === 200) {
            this.imageUrl = `https:${data.Location}`

            // 隐藏
            this.showProgress = false
            // 拿到第三方云服务器返回的url，传递给父组件
            // this.$emit('img-url', this.imageUrl)
            this.$emit('input', this.imageUrl)
          }
        })
      }
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isPNG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isPNG && isLt2M
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
/* 图片上传进度条样式 */
.progress {
  position: absolute;
  display: flex;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);
  background: #fff;
}
</style>
